﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class PGJGListControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        CustomGridView CustomGridList1;
        DropDownList RWND, XYDJ, ZZDJ;
        Label Label1, Label2, Label3;
        Button Button2;

        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            InitControl();
            CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            if (!base.Page.IsPostBack)
            {
                BindDll();
                string nowyear = System.DateTime.Now.Year.ToString();
                for (int i = 0; i < RWND.Items.Count; i++) {
                    if (RWND.Items[i].Text == nowyear) {
                        RWND.SelectedIndex = i;
                        RWND.SelectedValue = nowyear;
                    }
                }
                Bind();
            }
            CustomGridList1.OnLoadData += new EventHandler(CustomGridList1_OnLoadData);
            CustomGridList1.RowDeleting += new GridViewDeleteEventHandler(CustomGridList1_RowDeleting);
            Button btnsearch = (Button)FindControl("btnsearch");
            btnsearch.Click += new EventHandler(Go_Search);
            Button2 = (Button)FindControl("Button2");
            Button2.Click += new EventHandler(Btn_CopyCom);
            if (!_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsZSAdmin(userid) && !_EluZSDataProvider.IsSZSB(userid))
            {
                RWND.SelectedIndex = 0;
                RWND.SelectedValue = "";
                XYDJ.SelectedValue = "";
                ZZDJ.SelectedValue = "";

                CustomGridList1.Columns[8].Visible = false;
                Button2.Visible = false; 
                Label1.Visible = false; 
                Label2.Visible = false; 
                Label3.Visible = false;

                RWND.Visible = false; 
                XYDJ.Visible = false; 
                ZZDJ.Visible = false;
                Page.ClientScript.RegisterStartupScript(this.GetType(), "displaybtn", "<script>displaybtn();</script>");
            }
        }
        protected void InitControl() {
            RWND = (DropDownList)FindControl("RWND");
            XYDJ = (DropDownList)FindControl("XYDJ");
            ZZDJ = (DropDownList)FindControl("ZZDJ");
            Label1 = (Label)FindControl("Label1");
            Label2 = (Label)FindControl("Label2");
            Label3 = (Label)FindControl("Label3");
            Button2 = (Button)FindControl("Button2");
        }

        protected void Go_Search(object sender, EventArgs e)
        {
            Bind();
        }
        protected void Bind()
        {
            DataTable dt = _EluZSDataProvider.GetCompany("评估机构", GetSqlWhere());
            CustomGridList1.DataSource = dt;
            CustomGridList1.RecordCount = dt.Rows.Count;
            CustomGridList1.PageSize = 10;
            CustomGridList1.DataKeyNames = new string[] { "ID" };
            CustomGridList1.DataBind();
        }
        protected void CustomGridList1_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }
        protected void CustomGridList1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string strid = CustomGridList1.DataKeys[e.RowIndex].Value.ToString();
            bool del = _EluZSDataProvider.DeleteXmByID("zs_company", "id", strid);
            if (del)
            {
                Bind();
                Antu.Utilities.UtilitiesPage.AlertDialog("删除成功！", "");
            }
        }
        protected string GetSqlWhere()
        {
            string strWhere = string.Empty;
            TextBox dwmc = (TextBox)FindControl("dwmc");
            string rwyear = RWND.SelectedValue;
            string xytype = XYDJ.SelectedValue;
            string zztype = ZZDJ.SelectedValue;
            if (!string.IsNullOrEmpty(dwmc.Text.Trim()))
            {
                strWhere += string.Format(" and 单位名称 like '%{0}%'", dwmc.Text);
            }
            if (!string.IsNullOrEmpty(rwyear))
            {
                strWhere += string.Format(" and 入围年度 = '{0}'", rwyear);
            }
            if (!string.IsNullOrEmpty(xytype))
            {
                strWhere += string.Format(" and 信用等级 = '{0}'", xytype);
            }
            if (!string.IsNullOrEmpty(zztype))
            {
                strWhere += string.Format(" and 房地产估价机构资质等级 = '{0}'", zztype);
            }
            if (!_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsZSAdmin(userid) && !_EluZSDataProvider.IsSZSB(userid))
            {
                strWhere += string.Format(@" and id in (select cid from zs_user_company where 
userid in (select id from zs_user where iscomuser=0) and userid ='{0}')", userid);
            }
            strWhere += " order by addtime desc";
            return strWhere;
        }
        protected void BindDll()
        {
            DataTable dt = _EluZSDataProvider.GetComDis("入围年度", "评估机构");
            DataRow dr = dt.NewRow();
            dt.Rows.InsertAt(dr, 0);
            RWND.DataSource = dt;
            RWND.DataValueField = "入围年度";
            RWND.DataTextField = "入围年度";
            RWND.DataBind();
            DataTable dt1 = _EluZSDataProvider.GetComDis("信用等级", "评估机构");
            XYDJ.DataSource = dt1;
            XYDJ.DataValueField = "信用等级";
            XYDJ.DataTextField = "信用等级";
            XYDJ.DataBind();
            DataTable dt2 = _EluZSDataProvider.GetComDis("房地产估价机构资质等级", "评估机构");
            DataRow dr2 = dt2.NewRow();
            dt2.Rows.InsertAt(dr2, 0);
            ZZDJ.DataSource = dt2;
            ZZDJ.DataValueField = "房地产估价机构资质等级";
            ZZDJ.DataTextField = "房地产估价机构资质等级";
            ZZDJ.DataBind();
        }
        protected void Btn_CopyCom(object sender, EventArgs e)
        {
            string values = "CopyCompany.aspx";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "selecthouse", string.Format("<script>SelectHouse('{0}');</script>", values));
            Bind();
        }
    }
}


